import request from '@/utils/request.ts'
import type {
  RoleListResponseData,
  RoleData,
  PermissionListResponseData,
} from '@/api/acl/role/type.ts'

enum API {
  //获取角色列表
  ROLE_LIST_URL = '/admin/acl/role',
  //删除角色
  DELETE_ROLE_URL = '/admin/acl/role/remove',
  //添加角色
  ADD_ROLE_URL = '/admin/acl/role/save',
  //更新角色
  UPDATE_ROLE_URL = '/admin/acl/role/update',

  //根据角色获取菜单权限
  GET_MENU_URL = '/admin/acl/permission/toAssign',
  //保存角色权限
  SAVE_ROLE_URL = '/admin/acl/permission/doAssign',
}

//获取角色列表
export const reqRoleList = (page: number, limit: number, roleName?: string) =>
  request.get<any, RoleListResponseData>(
    API.ROLE_LIST_URL +
      `/${page}/${limit}${roleName ? `?roleName=${roleName}` : ''}`,
  )
export const reqDeleteRole = (id: number) =>
  request.delete<any, any>(API.DELETE_ROLE_URL + `/${id}`)
export const reqAddRole = (role: RoleData) =>
  request.post<any, any>(API.ADD_ROLE_URL, role)
export const reqUpdateRole = (role: RoleData) =>
  request.put<any, any>(API.UPDATE_ROLE_URL, role)

//根据角色获取菜单权限
export const reqGetMenu = (roleId: number) =>
  request.get<any, PermissionListResponseData>(API.GET_MENU_URL + `/${roleId}`)
//保存角色权限
export const reqSaveRole = (roleId: number, permissionId: number[]) =>
  request.post<any, any>(
    API.SAVE_ROLE_URL + `?roleId=${roleId}&permissionId=${permissionId}`,
  )
